package com.livestocksolutions.ui;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

/* -- Version History --
 *  1.0 : 10 September 2012 - Initial version.
 */

/**
 * A base class to create the layout for a Mob Object
 * @author Adam Parr
 * @version 1.0
 * 
 * @.modified 09 September 2012
 */
public class ViewMobArrayAdapter extends ViewGroupMobArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 4;
	/**
	 * ID of the Paddock in which this mob is in position within the 
	 * String[position][{@value #LOCATION}] array passed in the Constructor.
	 */
	public static final int LOCATION = 3;
	/**
	 * The list of paddocks that will be selectable.
	 */
	protected String[][] paddock = new String[][]{};

	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Group data to represent in the ListView.
	 */
	public ViewMobArrayAdapter(BaseActivity context, String[] values) {
		super(context, new String[][]{values});
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(textViewResourceId==0){
			setView(R.layout.item_mob_view);
		}
		View rowView = super.getView(position, convertView, parent);
		
		View mobPaddockList = rowView.findViewById(R.id.mobPaddockList);
		switch(textViewResourceId){
			case R.layout.item_mob_modify:
				((Spinner)mobPaddockList).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
						values[position][LOCATION]=paddock[index][ID];
					}
		
					public void onNothingSelected(AdapterView<?> parent) {}
				});
				
				String[] list = new String[paddock.length];
				for(int x=0; x< paddock.length; x++){
					list[x]=paddock[x][NAME];
				}
				ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, list);
				((Spinner)mobPaddockList).setAdapter(spinnerArrayAdapter);
				
				// Determine the Paddock this Mob is in
				for(int x=0; x < paddock.length; x++){
					if(values[0][LOCATION]==null || paddock[x][ID].equals(values[0][LOCATION])){
						((Spinner)mobPaddockList).setSelection(x);
						break;
					}
				}
			break;
			case R.layout.item_mob_view:
				// Determine the Paddock this Mob is in
				for(int x=0; x < paddock.length; x++){
					if(values[0][LOCATION]==null || paddock[x][ID].equals(values[0][LOCATION])){
						((TextView)mobPaddockList).setText(paddock[x][NAME]);
						break;
					}
				}
				
		}
		return rowView;
	}
	
	
	/**
	 * Display the View that will allow the modification of some fields.<br>
	 * This should be used when choosing to modify the Mob.
	 */
	public void setModifyMob(){
		setView(R.layout.item_mob_modify);
	}
	
	/**
	 * Display the View that will only display the Mob details.
	 */
	public void setViewMob(){
		setView(R.layout.item_mob_view);
	}
	
	/**
	 * Set the list of Paddocks that this mob could belong to.<br>
	 * The structure of this list is expected to be the First Dimension containing each Paddock,
	 * the Second Dimension is structured with {@link #ID} and {@link #NAME}, with ID containing
	 * a String representation of the long id of that Paddock, and NAME containing the name of the
	 * Paddock.
	 * @param paddockList The String Array to use as the list of paddocks.
	 */
	public void setPaddockList(String[][] paddockList){
		paddock = paddockList;
	}
}
